# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: jshin


class LastFireTimestamp(object):
  def __init__(self, config):
    self._config = config
    self._last_sell_timestamp = 0
    self._last_buy_timestamp = 0
    self._last_timestamp = 0
    self._num_sell_fires = 0
    self._num_buy_fires = 0
    self._num_fires = 0

  @property
  def config(self):
    return self._config

  @property
  def num_fires(self):
    return self._num_fires

  @property
  def num_sell_fires(self):
    return self._num_sell_fires

  @property
  def num_buy_fires(self):
    return self._num_buy_fires

  def _within_sell_posting_period(self, timestamp):
    in_period = False
    if (timestamp - self._last_sell_timestamp <= self.config.max_posting_period):
      in_period = True
    return in_period

  def _within_buy_posting_period(self, timestamp):
    in_period = False
    if (timestamp - self._last_buy_timestamp <= self.config.max_posting_period):
      in_period = True
    return in_period

  def _within_sell_update_period(self, timestamp):
    in_period = False
    if (timestamp - self._last_sell_timestamp <= self.config.order_update_period):
      in_period = True
    return in_period

  def _within_buy_update_period(self, timestamp):
    in_period = False
    if (timestamp - self._last_buy_timestamp <= self.config.order_update_period):
      in_period = True
    return in_period

  def _within_buy_sell_fill_cooldown_period(self, timestamp):
    in_period = False
    if (timestamp - self._last_buy_timestamp <= self.config.fill_cooldown_period) or \
            (timestamp - self._last_sell_timestamp <= self.config.fill_cooldown_period):
      in_period = True
    return in_period

  def set_last_sell_fire(self, timestamp):
    self._last_sell_timestamp = timestamp
    self._num_sell_fires += 1
    if self._last_timestamp < timestamp:
      self._num_fires += 1
      self._last_timestamp = timestamp
    else:
      assert self._last_timestamp == timestamp, (self._last_timestamp, timestamp)

  def set_last_buy_fire(self, timestamp):
    self._last_buy_timestamp = timestamp
    self._num_buy_fires += 1
    if self._last_timestamp < timestamp:
      self._num_fires += 1
      self._last_timestamp = timestamp
    else:
      assert self._last_timestamp == timestamp, (self._last_timestamp, timestamp)
